
var HotlinkParser = {
  MARK_KEY_1 : "#key.1#",

  MARK_TEXT : "#text#",
  
  PATTERN_SITE_MAP : new Array(
    new Array("([hH][Tt][Tt][Pp][Ss]?://([\\-\\w\\.]+)+(:\\d+)?(?:/[^ \\\"\\t\\n\\r\\u0100-\\uffff<]*)?)", "<a class=\"hotlink\" href=\"#key.1#\" target=\"_foreign\" title=\"#text#\"><img src=\"templates/default/images/ext_link.png\" alt=\"::URL::\" width=\"14px\" height=\"14px\"></img></a>"),
    new Array("[\(]#(\\d+)[\)]", "<a class=\"hotlink\" href=\"http://forum.htkou.net/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@htkou", "<a class=\"hotlink\" href=\"http://forum.htkou.net/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@rolia", "<a class=\"hotlink\" href=\"http://www.rolia.net/f/post.php?pno=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@shuigang", "<a class=\"hotlink\" href=\"http://yantange.com/forum/showpost.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("[\(]y(\\d+)[\)]", "<a class=\"hotlink\" href=\"http://yantange.com/forum/showpost.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@google", "<a class=\"hotlink\" href=\"http://www.google.ca/search?q=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@youtube", "<a class=\"hotlink\" href=\"http://www.youtube.com/watch?v=#key.1#\" target=\"_forgein\">#text#</a>")
  ),
  run : function() {
    this.convertPosts();
  },

  convertPosts : function() {
    var contentElement = document.getElementById("content");
    var posts = contentElement.getElementsByTagName("span")
  
    for(var i=0; i<posts.length; i++) {
      if(posts[i].className.indexOf('thread_no_text') >= 0
      || posts[i].className.indexOf('threadnew_no_text') >=0
      || posts[i].className.indexOf('thread-sticky_no_text') >=0
      || posts[i].className.indexOf('threadnew-sticky_no_text') >=0
      || posts[i].className.indexOf('replynew_no_text') >=0
      || posts[i].className.indexOf('reply_no_text') >=0 ) {
        this.convertPost(posts[i]);
      }
    }
  },
  
  convertPost : function (postElement) {
    var changed = false;
    var post = postElement.innerHTML;

    for(var i=0; i< this.PATTERN_SITE_MAP.length; i++) {
      var pattern = this.PATTERN_SITE_MAP[i][0];
      var template = this.PATTERN_SITE_MAP[i][1];

      var reg = new RegExp(pattern, "g");
      var result = reg.exec(post);
      var results = new Array();
      while(result != null) {
        results[results.length] = result;
        result = reg.exec(post);
      }

      var index = 0;
      for(var j=0; j< results.length; j++) {
        var link = template.replace(this.MARK_KEY_1, results[j][1])
                           .replace(this.MARK_TEXT, results[j][0]);
        index = post.indexOf(results[j][0], index);
        var before = post.substring(0, index);
        var after = post.substring(index, post.length);
        //alert("before: " + before);
        //alert("after: " + after);
        post = before + after.replace(results[j][0], link);
        index = index+link.length;
        changed = true;
      }
    }
    if(changed) {
      postElement.innerHTML = post;
    }
  }
}

HotlinkParser.run();
